04 - R framework with IMPACT - session 4

Author

Yann Say

Published

May 7, 2024

Recap

library(analysistools)
library(dplyr)

my_data <- analysistools::analysistools_MSNA_template_data

sampling_frame <- data.frame(
  strata = c("admin1a", "admin1b", "admin1c"),
  population = c(100000, 200000, 300000)
)

my_data <- my_data %>% 
  add_weights(sampling_frame, "admin1", "strata", "population")

my_loa <- analysistools::analysistools_MSNA_template_loa

my_design <- srvyr::as_survey_design(my_data, weights = "weights", strata = "admin1")
my_results <- create_analysis(my_design, loa = my_loa, sm_separator = "/")

Outputs - Wide tables

library(presentresults)

my_results_table <- my_results$results_table

The framework is built around 4 steps: cleaning, composition, analysis, outputs

  • Cleaning: Any manipulation to go from the raw data to the clean data
  • Composition: Any manipulation before the analysis e.g. adding indicators, adding information from loop, main dataset, or any other dataset (e.g. previous round), aok aggregation, etc.
  • Analysis: Any manipulation regarding only the analysis.
  • Outputs: Any manipulation to format the outputs. Outputs are created from the results table, from the stat + analysis key

The following section will present some introduction about the outputs.

There are currently two types of table:

  • one that have the variables in the rows and the disagregation in the columns,
  • one that have the disagregation in the rows and the variables in the columns.

There are two steps to turn a results table:

  • Turn the long results table to a large results table.
  • Format and export it to Excel.
create_*

create_* functions will create, transform something, e.g. creating a cleaning log with the checks to be filled, create analysis results table, create an output.

Outputs from create_* functions outputs can be in different shape, format, etc.

create_* function is catch-all.

create_*_group_x_variable

Wide table with the groups in the rows and the variables in the columns.

my_results_table %>% 
  create_table_group_x_variable() %>% 
  create_xlsx_group_x_variable(file_path = "outputs/04 - example - group_x_variable.xlsx", overwrite = T)

create_*_variable_x_group

Wide table with the variables in the rows and the groups in the columns.

my_results_table %>% 
  create_table_variable_x_group() %>%
  create_xlsx_variable_x_group(file_path = "outputs/05 - example - variable_x_group.xlsx", overwrite = T)

Tabular HTML

The folders 05 - reach_tabular_html_* are example of Quarto projects. They can be used to produce some tables in the html format.

The params in the header can be change. Use Render after to create the html output.

Note

The following is work in progress. It will later become something like create_html_variable_x_group.

Exercise

Exercise 1

  • Create an excel table with the strata in the rows and the variables in the columns.
library(presentresults)
exercise_outputs <- readxl::read_excel("inputs/10 - exercise - analysis_to_review.xlsx")
exercise_outputs %>% 
  create_table_group_x_variable() %>% 
  create_xlsx_group_x_variable(file_path = "outputs/06 - correction - group_x_variable_table.xlsx", overwrite = T) 

Exercise 2

  • Try the tabular html output.
  • Try to edit the authors, RCID and the introduction.